package org.hs.phd.odi.tracking.tracker;

import java.awt.BasicStroke;
import java.awt.Paint;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.List;

import org.hs.phd.common.charting.SeriesInfo;
import org.hs.phd.common.simpleplayer.ImageSequenceFrame;
import org.hs.phd.common.simpleplayer.overlay.PolygonalOverlay;

public class TrackerGroup {

	public static class PaintableTracker implements SeriesInfo{
		public final Tracker tracker;
		public final Paint paint;
		
		public PaintableTracker(Tracker tracker, Paint paint) {
			super();
			this.tracker = tracker;
			this.paint = paint;
		}

		@Override
		public String getName() {
			return tracker.getName();
		}

		@Override
		public Paint getPaint() {
			return paint;
		}
	}
	
	private List<PaintableTracker> trackers;
	private boolean isInitialized;
	
	public TrackerGroup(){
		trackers = new ArrayList<PaintableTracker>();
		isInitialized = false;
	}

	public void add(Tracker tracker, Paint paint) {
		trackers.add(  new PaintableTracker( tracker, paint ) );
	}

	public void initTrackers( Polygon initialTrackShape, ImageSequenceFrame frame) {
		int i = 0;
		for(PaintableTracker paintable : trackers){
			TrackWindow track = paintable.tracker.init(initialTrackShape, frame.getFrameImage());
			frame.addOverlay( this, i++, new PolygonalOverlay(track.getAsPolygon(), paintable.paint, new BasicStroke(2)));
			isInitialized = true;
		}
	}

	public void dropTracks() {
		// TODO Auto-generated method stub
		
	}

	public void updateWithNextFrame(ImageSequenceFrame frame) {

		if(!isInitialized) return;
		
		int i = 0;
		for(PaintableTracker paintable : trackers){
			TrackWindow track = paintable.tracker.track(frame.getFrameImage());
			frame.addOverlay(this, i++, new PolygonalOverlay(track.getAsPolygon(), paintable.paint));
		}
	}
	
	public List<PaintableTracker> getList(){
		return trackers;
	}

}
